package springboot.springMVC.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springboot.springMVC.model.MessageInfo;
import springboot.springMVC.service.MessageService;


import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wall")
public class MessageWallController {
    @Autowired
    private MessageService messageService;

    List<MessageInfo>list=new ArrayList<>();

    @RequestMapping(value="/publish" ,produces="application/json")
    public String publish(@RequestBody MessageInfo a){
        if(StringUtils.hasLength(a.getFrom())&&StringUtils.hasLength(a.getTo())&&StringUtils.hasLength(a.getSay()))
        {
//            list.add(a);
            messageService.addMessage(a);
            return "{\"ok\":1}";//错因，一开始忘记加上大括号了，识别不了json
        }
        else return "{\"ok\":0}";

    }



    @RequestMapping(value = "/view")
    public List<MessageInfo> view(){
       return messageService.getMessages();
//        return list;
    }
}
